package com.zdkj.subway.st.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.entity.SwStTicketBorrowReturn;
import com.zdkj.subway.st.service.SwStTicketBorrowReturnService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@AllArgsConstructor
@RequestMapping("/swStTicketBorrowReturn")
@Api(value = "swStTicketBorrowReturn", tags = "卡票借用与归还")
public class SwStTicketBorrowReturnController {
    private final SwStTicketBorrowReturnService swStTicketBorrowReturnService;

    @GetMapping("/{id}")
    public R getById(@PathVariable String id) {
        return R.ok(swStTicketBorrowReturnService.getById(id));
    }

    @GetMapping("/page")
    public R<IPage> getDictPage(Page page, SwStTicketBorrowReturn swStTicketBorrowReturn) {
        LambdaQueryWrapper<SwStTicketBorrowReturn> lambdaQueryWrapper = Wrappers.query(swStTicketBorrowReturn).lambda();
        String borrowTimeDTO = swStTicketBorrowReturn.getBorrowTimeDTO();
        String returnTimeDTO = swStTicketBorrowReturn.getReturnTimeDTO();
        if (StringUtils.isNotBlank(borrowTimeDTO)) {
            lambdaQueryWrapper.likeRight(SwStTicketBorrowReturn::getBorrowTime, borrowTimeDTO);
        }
        if (StringUtils.isNotBlank(returnTimeDTO)) {
            lambdaQueryWrapper.likeRight(SwStTicketBorrowReturn::getReturnTime, returnTimeDTO);
        }
        lambdaQueryWrapper.orderByDesc(SwStTicketBorrowReturn::getBorrowNumber);
        return R.ok(swStTicketBorrowReturnService.page(page, lambdaQueryWrapper));
    }

    @SysLog("卡票借用与归还记录")
    @PostMapping
    public R save(@Valid @RequestBody SwStTicketBorrowReturn swStTicketBorrowReturn) {
        PigxUser user = SecurityUtils.getUser();
        swStTicketBorrowReturn.setCreatorId(user.getId());
        swStTicketBorrowReturn.setCreatorName(user.getUsername());
        return R.ok(swStTicketBorrowReturnService.save(swStTicketBorrowReturn));
    }

    @SysLog("删除卡票借用与归还记录")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(swStTicketBorrowReturnService.removeById(id));
    }

    @PutMapping
    @SysLog("修改卡票借用与归还记录")
    public R updateById(@RequestBody SwStTicketBorrowReturn swStTicketBorrowReturn) {
        return R.ok(swStTicketBorrowReturnService.updateById(swStTicketBorrowReturn));
    }

}
